package com.google.client;

import java.util.List;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.Label;

public class ProductList extends Composite {
	private List<Product> rows;
	private MarketingServiceAsync marketingSvc = GWT.create(MarketingService.class);
	public ProductList() {
		
		FlexTable flexTable = new FlexTable();
		initWidget(flexTable);
		
		/*for (int i = flexTable.getRowCount(); i > 0; i--) {
			flexTable.removeRow(0);
		}
		*/
		Label lblName = new Label("name");
		flexTable.setWidget(0, 0, lblName);
		
		Label lblPrice = new Label("price");
		flexTable.setWidget(0, 1, lblPrice);
		
		Label lblDetails = new Label("details");
		flexTable.setWidget(0, 2, lblDetails);

	
		getList();
		
		int i = 1;
		for (Product Product : rows) {
			//flexTable.setText(i, 0, Long.toString(Product.getPid()));
			flexTable.setText(i, 0, Product.getName());
			flexTable.setText(i, 1, Double.toString(Product.getPrice()));
			//flexTable.setWidget();
			i++;
		}

	}
private void getList() 
{
		// Initialize the service proxy.
		    if (marketingSvc == null) {
		    	marketingSvc = GWT.create(MarketingService.class);
		    }

		    // Set up the callback object.
		    AsyncCallback<List<Product>> callback = new AsyncCallback<List<Product>>() {
		      public void onFailure(Throwable caught) {
		  		Window.alert(caught.getMessage());

		         }

		      public void onSuccess(List<Product> result) {
		    	  rows = result;
		    	  for (Product p : rows) {
		  			System.out.println(p.getName());
		  		}
		      }
		    };

		    // Make the call to the stock price service.
		    marketingSvc.getProductList(callback);
	}

	
}